package com.mycompany.project.client.jack;


import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.http.client.URL;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.jsonp.client.JsonpRequestBuilder;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;




/**
 * @author Jack
 * Weather class creates the weather widget from the JSON feed
 */
public class Weather {
    /**
     * The vertical panel containing the weather feed.
     */
    private final transient VerticalPanel outp = new VerticalPanel();
    /**
      * @return outp is the return panel.
      */
     public final VerticalPanel getPanel() {
          return outp;
     }
     /**
      * Weather widget takes information from JSON feed.
      */
     public Weather() {
     String url =
    "http://api.wunderground.com/api/37ddac705e45b702/conditions/q/55811.json?";
          url = URL.encode(url);

          final JsonpRequestBuilder jsonp = new JsonpRequestBuilder();

          jsonp.setCallbackParam("callback");
          jsonp.requestObject(url, new AsyncCallback<JavaScriptObject>() {
              @Override
              public void onFailure(final Throwable caught) {
                  Window.alert("JSONP onFailure");
              }
              @Override
              public void onSuccess(final JavaScriptObject temporary) {
              JSONObject obj = new JSONObject(temporary);
              String result = obj.toString();
                 String sAll = result;
            JSONObject jArray =
                   (JSONObject) JSONParser.parseLenient(sAll);
               JSONValue jTry = jArray.get("current_observation");

                 JSONObject jObject =
            (JSONObject) JSONParser.parseLenient(jTry.toString());
               JSONValue temp = jObject.get("temp_f");
            JSONValue visibility = jObject.get("visibility_km");
               JSONValue wind = jObject.get("wind_string");
            JSONValue weather = jObject.get("weather");

             String sTemp = temp.toString();
               String sVisibility = visibility.toString();

               HTML output = new HTML("<center>Temp Farenheit: " + sTemp
            + " degrees" + "<br>" + "Visibility Distance: " + sVisibility
            + " km</br>" + "Wind: " + wind.toString() + "<br>"
            + "Weather: " + weather.toString() + "</center></br>");

             outp.add(new Image("images/wundergroundLogo_4c.jpg"));
                outp.add(output);
                outp.setSize("320px", "290px");
                outp.setVerticalAlign(VerticalAlignment.MIDDLE);
             outp.setHorizontalAlign(HorizontalAlignment.CENTER);
              }
  });
 }

}
